package easybooking.db;

import easybooking.FlightServer;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Managing the connection to the database.
 *
 * @author Mark Roman Stehle
 */
// <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
// #[regen=yes,id=DCE.FB5FD68C-86DC-7D25-03F7-290AAD8AE5E7]
// </editor-fold> 
public class DBConnection {

    /**
     * The class <code>DBConnection</code> will be instantiated only once.
     * This is called <b>Singleton</b>, because of the <code>static</code>-tag,
     * the variable exists only once for all instances of this class. The
     * variable saves the only instance of this class.
     *
     * @see AirlineMapper.airlineMapper()
     * @see AirportMapper.airportMapper()
     * @see BookingMapper.bookingMapper()
     * @see CustomerMapper.customerMapper()
     * @see FlightMapper.flightMapper()
     * @see PlaneMapper.planeMapper()
     * @see SeatMapper.seatMapper()
     * @see TypeMapper.typeMapper()
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker ">
    // #[regen=yes,id=DCE.6440F80D-EBE7-8455-2BFC-4596B7794EF5]
    // </editor-fold> 
    private static Connection con = null;
    private static String dbHost = "mars.iuk.hdm-stuttgart.de";
    private static String dbName = "u-ms289";
    private static String dbUser = "ms289";
    private static String dbPassword = "Ei2jai8wah";

    public static String getDbHost() {
        return dbHost;
    }

    public static void setDbHost(String dbHost) {
        DBConnection.dbHost = dbHost;
    }

    public static String getDbName() {
        return dbName;
    }

    public static void setDbName(String dbName) {
        DBConnection.dbName = dbName;
    }

    public static String getDbPassword() {
        return dbPassword;
    }

    public static void setDbPassword(String dbPassword) {
        DBConnection.dbPassword = dbPassword;
    }

    public static String getDbUser() {
        return dbUser;
    }

    public static void setDbUser(String dbUser) {
        DBConnection.dbUser = dbUser;
    }

    /**
     * The URL for connecting to the database, the URL contains the address
     * of the server, the database name as well as the username and the password.
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.4E748B54-619E-7944-19C1-A50CD12BE9B7]
    // </editor-fold>
    private static String generateUrl() {
        String url = "jdbc:mysql://" + dbHost + "/" + dbName + "?user=" + dbUser + "&password=" + dbPassword;
        return url;
    }

    /**
     * The static method can be called with
     * <code>DBConnection.connection()</code>.
     * This guarantees the singleton property by checking so that only one
     * instance of the <code>DBConnection</code> exists.

     * <b>Conclusion:</b> The <code>DBConnection</code> should not be
     * instantiated with <code>new</code>, but rather by calling the static
     * method.

     * @return The <code>DBConnection</code> object.
     * @see con
     */
    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.EF1421BF-0E2C-A22D-AF04-29CEE1C85FA7]
    // </editor-fold> 
    public static Connection connection() {
        if (con == null) {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                con = DriverManager.getConnection(generateUrl());
            } catch (SQLException e1) {
                con = null;
                //e1.printStackTrace();
            } catch (InstantiationException e) {
                con = null;
                // e.printStackTrace();
            } catch (IllegalAccessException e) {
                con = null;
                // e.printStackTrace();
            } catch (ClassNotFoundException e) {
                con = null;
                // e.printStackTrace();
            }
        }
        return con;
    }

    public static void unsetDBConnection() {
        con = null;
        dbHost = "";
        dbName = "";
        dbUser = "";
        dbPassword = "";
    }
}
